package com.darwin.darwinUI.controls
{
	import com.darwin.darwinUI.assist.Group;
	import com.darwin.darwinUI.assist.IGroupItem;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.general.DarwinFont;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 简易选项卡组件，IGroupItem的一种简单实现。
	 * <br/>使用时先实例化一个Group，把Tab注册进去使用
	 * <br/>想实现更丰富效果可以选择自行实现IGroupItem接口。
	 * @author Roland
	 */
	public class Tab extends UIComponent implements IGroupItem
	{
		private var _selected:Boolean = false;
		private var _group:Group;

		private var _label:Label;
		private var _selectedSkin:DisplayObject;
		private var _unselectedSkin:DisplayObject;
		private var _mouseOverSkin:DisplayObject;
		
		private var _labelText:String;
		private var _labelFont:DarwinFont;
		/**
		 *	 实例化一个选项卡
		 * @param labelText 选项卡标签文本
		 * @param labelFont 选项卡标签文本字体
		 * @param selectedSkin 选项卡被选中时使用的皮肤
		 * @param unselectedSkin 选项卡未选中时使用的皮肤
		 * 
		 */
		public function Tab(labelText:String = "",labelFont:DarwinFont = null,selectedSkin:DisplayObject = null,unselectedSkin:DisplayObject = null,mouseOverSkin:DisplayObject = null)
		{
			_labelText = labelText;
			_labelFont = labelFont;
			_selectedSkin = selectedSkin;
			_unselectedSkin = unselectedSkin;
			_mouseOverSkin = mouseOverSkin;
			super();
			this.backgroundAuto = true;
			this.background = _unselectedSkin;
			this.buttonMode = true;
		}
		
		protected function onTabClick(event:MouseEvent):void
		{
			if(selected == false)
			{
				selected = true;
			}
		}
		
		
		protected function onOver(event:MouseEvent):void
		{
			if(_mouseOverSkin)
			{
				this.background = _mouseOverSkin;
			}
		}
		
		protected function onOut(event:MouseEvent):void
		{
			if(_mouseOverSkin)
			{
				this.background = _selected ? _selectedSkin : _unselectedSkin;
			}
		}
		
		override protected function initUIComponent():void
		{
			addEventListener(MouseEvent.CLICK,onTabClick);
			addEventListener(MouseEvent.MOUSE_OVER,onOver);
			addEventListener(MouseEvent.MOUSE_OUT,onOut);
			super.initUIComponent();
		}

		
		override protected function onAddToStage(event:Event):void
		{
			super.onAddToStage(event);
			_label = new Label(_labelText,_labelFont);
			addChild(_label);
		}
		
		public function get group():Group
		{
			return _group;
		}
		
		public function set group(value:Group):void
		{
			_group = value;
		}

		public function set immediateSelected(value:Boolean):void
		{
			_selected = value;
			super.background = _selected ? _selectedSkin : _unselectedSkin;
		}
		
		/**
		 *	 选项卡是否选中
		 */
		public function get selected():Boolean
		{
			return _selected;
		}
		
		/**
		 * @private
		 */
		public function set selected(value:Boolean):void
		{
			if(group){
				group.itemSelectActionStart(this,value);
			}
		}
		
		//可覆盖此方法来实现动画效果，效果完成后回调group.itemSelectActionEnd()方法
		public function selectAction(selected:Boolean):void
		{
			//Tab动画过程//////////
			
			//////////////////////
			if(group){
				group.itemSelectActionEnd(this,selected);
			}
		}
		
		public function groupSelectionChange(curruntSelection:IGroupItem):void
		{
			
		}

		/**
		 *	选中状态时的皮肤 
		 */
		public function get selectedSkin():DisplayObject
		{
			return _selectedSkin;
		}

		/**
		 * @private
		 */
		public function set selectedSkin(value:DisplayObject):void
		{
			_selectedSkin = value;
		}

		/**
		 *	未选中状态时的皮肤 
		 */
		public function get unselectedSkin():DisplayObject
		{
			return _unselectedSkin;
		}

		/**
		 * @private
		 */
		public function set unselectedSkin(value:DisplayObject):void
		{
			_unselectedSkin = value;
		}
		
		/**
		 *	 鼠标移入时使用的皮肤 
		 */
		public function get mouseOverSkin():DisplayObject
		{
			return _mouseOverSkin;
		}
		
		/**
		 * @private
		 */
		public function set mouseOverSkin(value:DisplayObject):void
		{
			_mouseOverSkin = value;
		}
		
		/**
		 *	显示在选项卡上的文本标签
		 */
		public function get label():Label
		{
			return _label;
		}

		/**
		 * @private
		 */
		public function set label(value:Label):void
		{
			_label = value;
		}

		private var _rectVisible:Boolean = false;
		/**
		 * 显示该对象矩形 测试用 
		 * @param vaule
		 * 
		 */		
		override public function set rectVisible(vaule:Boolean):void
		{
			_rectVisible = vaule;
			this.graphics.clear();
			if(_rectVisible==true)
			{
				this.graphics.lineStyle(2,0xff0000);
				this.graphics.beginFill(0xff0000,0);
				this.graphics.drawRect(0,0,width,height);
				this.graphics.endFill();
			}
		}
		
		override public function get rectVisible():Boolean
		{
			return _rectVisible;
		}


	}
}